using LinearAlgebra, Kronecker 
using Statistics
using Plots
using StatsPlots
using LaTeXStrings
using DataFrames, DelimitedFiles 
using RCall
################################# Settings

#Algorithm settings
module params_solver

    x_a_H = 0.00001
    x_b_H = 100000
    iter_H_max = 5000
    xd_tol_H = 0.00001

    step_size = 0.125
    max_iterations = 5000
    start_price_multiplier = 10
    xd_tol = 0.00001

end

#Policy settings
module params_policy
    
    policy = "lf"

    import XLSX
    using Kronecker
    filename_policy = "aux_params/specs.xlsx"

    #Dimensions
    df = XLSX.readxlsx(filename_policy)["policy_ur"] 
    nI = size(df[:][2:end,1],1) 
    nN = 2 
    df = XLSX.readxlsx(filename_policy)["policy_dt"]
    nC = size(df[:][2:end,2],1) 
    nJ = size(df[:][2:end,2:end],2)

    #Policy
    tax_j_c = zeros(nC,nJ)
    tax_ij_c = kronecker(tax_j_c, ones(nI,1))
    tax_i = zeros(nI,nC)
    subsidy_i = zeros(nI)

end


################################# Baseline model

#Import model parameters
include("aux_functions/params_baseline.jl")

###### Imperfect competition
params_policy.conduct = "ic"
include("aux_functions/params_calibrated.jl")
include("aux_functions/solve_equilibrium.jl")
include("aux_functions/recover_nu.jl")

###### Perfect competition
params_policy.conduct = "pc"
include("aux_functions/solve_equilibrium.jl")


################################# Extension with mobility

#Import model parameters
include("aux_functions/params_mobility.jl")
include("aux_functions/params_calibrated.jl")

###### Imperfect competition
params_policy.conduct = "ic"
include("aux_functions/solve_equilibrium.jl")


################################# Set price-index-adjusted SCC
run(`Rscript aux_functions/set_scc.R`)
